package com.alanjaver.msgpush;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;

import com.google.android.gcm.GCMRegistrar;

public class MainActivity extends Activity
{
	TextView					mDisplay;
	AsyncTask<Void, Void, Void>	mRegisterTask;
	BroadcastReceiver			receiver	= new BroadcastReceiver()
											{
												@Override
												public void onReceive(Context context, Intent intent)
												{
													String newMessage = intent.getExtras()
															.getString(
																	CommonUtilities.EXTRA_MESSAGE);
													android.util.Log.d(CommonUtilities.TAG,
															"show new message : " + newMessage);
													if (mDisplay != null)
													{
														mDisplay.append(newMessage + "\n");
													}
												}
											};

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		// Make sure the device has the proper dependencies.
		GCMRegistrar.checkDevice(this);
		// Make sure the manifest was properly set - comment out this line
		// while developing the app, then uncomment it when it's ready.
		GCMRegistrar.checkManifest(this);

		setContentView(R.layout.activity_main);

		mDisplay = (TextView) findViewById(R.id.display);
		registerReceiver(receiver, new IntentFilter(CommonUtilities.DISPLAY_MESSAGE_ACTION));

		final String regId = GCMRegistrar.getRegistrationId(this);
		if (regId.equals(""))
		{
			// Automatically registers application on startup.
			GCMRegistrar.register(this, CommonUtilities.SENDER_ID);
		}
		else
		{
			// Device is already registered on GCM, check server.
			if (GCMRegistrar.isRegisteredOnServer(this))
			{
				// Skips registration.
				mDisplay.append(getString(R.string.already_registered) + "\n");
			}
			else
			{
				// Try to register again, but not in the UI thread.
				// It's also necessary to cancel the thread onDestroy(),
				// hence the use of AsyncTask instead of a raw thread.
				final Context context = this;
				mRegisterTask = new AsyncTask<Void, Void, Void>()
				{

					@Override
					protected Void doInBackground(Void... params)
					{
						boolean registered = ServerUtilities.register(context, regId);
						// At this point all attempts to register with the app
						// server failed, so we need to unregister the device
						// from GCM - the app will try to register again when
						// it is restarted. Note that GCM will send an
						// unregistered callback upon completion, but
						// GCMIntentService.onUnregistered() will ignore it.
						if (!registered)
						{
							GCMRegistrar.unregister(context);
						}
						return null;
					}

					@Override
					protected void onPostExecute(Void result)
					{
						mRegisterTask = null;
					}

				};
				mRegisterTask.execute(null, null, null);
			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	protected void onStop()
	{
		unregisterReceiver(receiver);
		super.onStop();
	}
}
